/**
 * 
 */
package edu.cse5311.operations;

import edu.cse5311.constants.Constants;
import edu.cse5311.entity.LogDisk;
import edu.cse5311.impl.Initialize;
import edu.cse5311.impl.Initialize.DISKINDEX;

/**
 * @author Karthik MG/ Shwetha Narayan
 * This class aids in
 * 
 */
public class HelperOperations {

	/**
	 * The find last checkpoint operation returns the index of the latest checkpoint on the disk
	 * If there is no checkpoint available then it returns zero
	 */

	public static int findLastCheckPoint() {
		int LastcheckPointIndex = 0;

		// check if there is any checkpoint already present in the log disk
		for (int i = 0; i < Initialize.logDisk.size(); i++) {
			LogDisk logdisk = Initialize.logDisk.get(i);
			if (logdisk.getOperation().getOperationName() == Constants.CHECKPOINT) {
				LastcheckPointIndex = i;
			}
		}
		/*
		 * //else if this operation is the first check point in the log disk if
		 * (LastcheckPointIndex == 0) { LastcheckPointIndex =
		 * Initialize.logDisk.size()+1; }
		 */
		return LastcheckPointIndex;

	}

	/**
	 * This function returns the index of an item on the disk
	 * It is equivalent to the concept of finding a key using a value
	 * @param itemName
	 * @return
	 */
	public static int getItemIndexOnDisk(String itemName) {

		for (DISKINDEX diskIndexenum : DISKINDEX.values()) {
			if (itemName.equals(diskIndexenum.toString()))
				return diskIndexenum.getI();
		}

		return -1;
	}

}
